package labox.innovation.gameserver.network.serverpackets;

import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastList;
import labox.innovation.config.Config;
import labox.innovation.gameserver.model.ItemInfo;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;

/**
 * ���� �ֲ�����
 * 
 * @author zjj
 * 
 */
public class InventoryUpdate extends L2GameServerPacket {

	private static final Logger _log = LoggerFactory.getLogger(InventoryUpdate.class.getName());

	private static final String _S__37_INVENTORYUPDATE = "[S] 0x72 InventoryUpdate";

	private List<ItemInfo> _items;

	public InventoryUpdate() {
		_items = new LinkedList<ItemInfo>();
		if (_log.isDebugEnabled()) {
			showDebug();
		}
	}

	/**
	 * @param items
	 */
	public InventoryUpdate(FastList<ItemInfo> items) {
		_items = items;
		if (_log.isDebugEnabled()) {
			showDebug();
		}
	}

	public void addItem(L2ItemInstance item) {
		if (item != null)
			_items.add(new ItemInfo(item));
	}

	public void addNewItem(L2ItemInstance item) {
		if (item != null)
			_items.add(new ItemInfo(item, 1));
	}

	public void addModifiedItem(L2ItemInstance item) {
		if (item != null)
			_items.add(new ItemInfo(item, 2));
	}

	public void addRemovedItem(L2ItemInstance item) {
		if (item != null)
			_items.add(new ItemInfo(item, 3));
	}

	public void addItems(List<L2ItemInstance> items) {
		if (items != null)
			for (L2ItemInstance item : items)
				if (item != null)
					_items.add(new ItemInfo(item));
	}

	private void showDebug() {
		for (ItemInfo item : _items) {
			_log.debug("oid:" + Long.toHexString(item.getObjectId()) + " item:" + item.getItem().getName() + " last change:" + item.getChange(), Config.SERVER_ID, "���� �ֲ�����");
		}
	}

	@Override
	protected final void writeImpl() {
		writeByte(PTL_INVENTORYUPDATE);
		int count = _items.size();
		showDebug();
		writeByte(count);
		for (ItemInfo item : _items) {
			writeLong(item.getObjectId()); // ObjectId
			writeByte(item.getChange()); // Update type : 01-add, 02-modify, 03-remove

			writeInt(item.getItem().getItemId()); // ItemId
			writeByte(item.getLocation()); // T1  //���õ�λ�ò��
			writeInt(item.getCount()); // Quantity

			writeByte(item.getEquipped()); // Equipped    : 00-No, 01-yes
			writeByte(item.getLocation());

		}

		FuzePcInstance pc = getClient().getActiveChar();

		if (pc == null) {
			return;
		}

		writeInt(pc.getMaxHp());
		writeInt(pc.getMaxMp());
		writeInt(pc.getMinPAtk());
		writeInt(pc.getMaxPAtk());
		writeInt(pc.getMinMAtk());
		writeInt(pc.getMaxMAtk());
		writeInt(pc.getPDef());
		writeInt(pc.getMDef());

		_items.clear();
		_items = null;
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.serverpackets.ServerBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _S__37_INVENTORYUPDATE;
	}
}
